﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Namespace Arwani
    Partial Class Backend_ManageCategory
        Inherits System.Web.UI.Page
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "../images/main_banner.png"
            If File.Exists(Server.MapPath("../Category/" & imgSrc)) Then
                strReturn = "../Category/" & imgSrc
            End If
            Return strReturn
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If IsPostBack Then Exit Sub
                If Request.QueryString("divid") <> "" Then Call loadData(Val(Request.QueryString("divid")))
                Dim strQ As String = "select * from division where divisionid= " & CInt(Request.QueryString("divid"))
                Dim arwCon As New ArwaniController
                Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
                If dtTable.Rows.Count > 0 Then
                    If Not IsDBNull(dtTable.Rows(0)("divisiontitle")) Then Me.lblHeading.Text = "Categories in Division : " & dtTable.Rows(0)("divisiontitle")
                End If
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub

        Public Sub loadData(ByVal divID As Integer)
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from category where divisionid=" & divID & " order by sortorder"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dgCats.DataSource = dtTable
            Me.dgCats.DataBind()

        End Sub

        Protected Sub dgCats_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgCats.ItemCommand
            If e.CommandName = "Edit" Then
                Response.Redirect("EditCategory.aspx?catid=" & CInt(e.CommandArgument) & "&divid=" & Request.QueryString("divid"))
            ElseIf e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = "delete from category where catid=" & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData(CInt(Request.QueryString("divid")))
            End If
        End Sub
         
        Protected Sub dgCats_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCats.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this category?')")
                Dim catID As Integer = dgCats.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'Category', 'catid','" & catID & "')")
            Catch ex As Exception

            End Try
        End Sub
        Protected Sub dgCats_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgCats.PageIndexChanged
            Me.dgCats.CurrentPageIndex = e.NewPageIndex
            Call loadData(Val(Request.QueryString("divid")))
        End Sub

        Protected Sub lnkAddNewCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddNewCategory.Click
            Response.Redirect("EditCategory.aspx?catid=0" & "&divid=" & Request.QueryString("divid"))
        End Sub

        Protected Sub lnkBackToDiv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBackToDiv.Click
            Response.Redirect("ManageDivision.aspx")
        End Sub
    End Class
End Namespace